 /* Copyright (C)
  * 2024 ZKLX Optoelectronic Technology Co.LTD
  * All rights reserved.
  */
 /**
  * @file misc_tool.cpp
  * @brief 
  * @author xuke
  * @version 1.0
  * @date 2024-09-28
  */

#include <iostream>
#include <sstream>
#include "misc_tool.h"

/*--------------------------------extern--------------------------------------------*/

/*--------------------------------globals-------------------------------------------*/

/*--------------------------------locals--------------------------------------------*/

/*--------------------------------funcs---------------------------------------------*/
/**
 * @brief URL解码接口
 * @param input
 * @return 0
 */
std::string misc_url_decode(const std::string& input)
{
	std::string output;
	output.reserve(input.size());

	for (size_t i = 0; i < input.size(); ++i) {
		if (input[i] == '%') {
			if (i + 3 <= input.size()) {
				int value = 0;
				std::istringstream is(input.substr(i + 1, 2));
				if (is >> std::hex >> value) {
					output += static_cast<char>(value);
					i += 2;
					continue;
				}
			}
			output += "%"; // Invalid encoding, keep original
		} else if (input[i] == '+') {
			output += ' '; // '+' decodes to space
		} else {
			output += input[i];
		}
	}
	return output;
}

